// logisticsInfo.js
const app = getApp()
import { requestApi } from '../../../api'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    warehouseList: [],
    logisticsList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.tradeNo = options.trade_no
    this.getLogisticsData()
  },

  // 获取物流详情
  getLogisticsData() {
    app.request({
      url: requestApi.logisticUrl,
      data: {
        trade_no: this.data.tradeNo,
      }
    }, res => {
      const data = res.original.data || {}
      const warehouseList = data.warehouse_status_schedule || []
      const logisticsList = data.logistics_list || []

      logisticsList.forEach(item => {
        // 在没有物流信息时，item.data值是物流官网地址
        if (!Array.isArray(item.data)) {
          item.data = []
        }
      })

      this.setData({
        warehouseList,
        logisticsList,
      })
    }, error => {
      wx.showModal({
        title: '提示',
        content: error,
        showCancel: false
      })
    })
  },


  // “复制”物流号到剪贴板
  onCopy(e) {
    wx.setClipboardData({
      data: e.currentTarget.dataset.no,
      success (res) {
        wx.showToast({
          title: '复制成功',
          icon: 'success',
          duration: 2000
        })        
      }
    })
  },
})
